package cc.z8g.browser.core.search

import android.text.Editable
import android.text.TextWatcher
import android.text.style.CharacterStyle
import android.text.style.ParagraphStyle

/**
 * 确保文本输入框中的文本内容保持纯文本状态
 */
class StyleRemovingTextWatcher : TextWatcher {
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit

    override fun afterTextChanged(e: Editable) {
        e.getSpans(0, e.length, CharacterStyle::class.java).forEach(e::removeSpan)
        e.getSpans(0, e.length, ParagraphStyle::class.java).forEach(e::removeSpan)
    }
}
